/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-6-22 上午10:56
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.recycleviewdemo.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.WebView;

import com.example.dqhl.recycleviewdemo.R;
import com.example.dqhl.recycleviewdemo.util.MyWebViewClient;
import com.example.dqhl.recycleviewdemo.util.StringUtil;

@SuppressLint("SetJavaScriptEnabled")
public class WebViewActivity extends AppCompatActivity {

    private WebView webview;
    private String[] imageUrls = StringUtil.returnImageUrlsFromHtml();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
//
        (webview) = (WebView) findViewById(R.id.webview);
        webview.getSettings().setAppCacheEnabled(true);
        webview.getSettings().setDatabaseEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        //启用支持javascript
        webview.getSettings().setJavaScriptEnabled(true);
//        webview.addJavascriptInterface(new MJavascriptInterface(this,imageUrls),"imagelistener");
        // 优先使用缓存
//        webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); //  不使用缓存 只从网络获取数据
//                    WebSettings webSettings = webview_detail.getSettings();
//                    webSettings.setUseWideViewPort(true); //设置webview推荐使用的窗口
        webview.getSettings().setLoadWithOverviewMode(true); //设置webview加载的页面的模式
//        webview.loadUrl("file:///android_asset/text.html");
        webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
        webview.loadUrl("http://a.mp.uc.cn/article.html?uc_param_str=frdnsnpfvecpntnwprdssskt&client=ucweb&wm_aid=c51bcf6c1553481885da371a16e33dbe&wm_id=482efebe15ed4922a1f24dc42ab654e6&pagetype=share&btifl=100");
//        webview.loadUrl("https://audio.leting.io/07a05cb8-4751-4433-bdda-96e40fe212da.mp3");
//        webview.loadUrl("https://m.amap.com");
        webview.setWebViewClient(new MyWebViewClient());
    }

    public class MJavascriptInterface {
        private Context context;
        private String[] imageUrls;

        public MJavascriptInterface(Context context, String[] imageUrls) {
            this.context = context;
            this.imageUrls = imageUrls;
        }

        @android.webkit.JavascriptInterface
        public void openImage(String img) {
            Intent intent = new Intent();
            intent.putExtra("imageUrls", imageUrls);
            intent.putExtra("curImageUrl", img);
            intent.setClass(context, ImageShowerActivity.class);
            context.startActivity(intent);
            for (int i = 0; i < imageUrls.length; i++) {
                Log.e("图片地址" + i, imageUrls[i].toString());
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        webview.loadData("<a></a>", "text/html", "utf-8");
    }
}
